package com.wzw.report;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.wzw.report.chart.BarStatisticData;
import com.wzw.report.chart.ChartHelper;
import com.wzw.report.chart.ChartResult;
import com.wzw.report.chart.PieStatisticData;
import com.wzw.report.chart.StatisticData;
import com.wzw.tcjh.SQLProcessor;

public class ReportImpl extends ReportBase {

	@Override
	public ReportResult getClickSourceReport() {
		ReportResult detail = new ReportResult();
		List<ChartResult> chartList = new ArrayList<ChartResult>();
		SelectCondition con = null;
		PieStatisticData chartInfo = null;
		List<StatisticData> mediaList = null;
		List<StatisticData> adList = null;
		ChartResult chartResult = null;
		List<List<StatisticData>> tableDataList = new ArrayList<List<StatisticData>>();
		try {
			// 媒体点击统计
			con = new SelectCondition();
			con.sql = "select m.m_id,m.m_name,sum(a.ad_hitcount) as hitcount" +
					" from adposition a" +
					" left join media m" +
					" on a.m_id=m.m_id" +
					" where projectcode='" + this.projectCode + "'" +
					" group by m.m_id,m.m_name";
			String[] colmun = {"m_id","m_name","hitcount"};
			con.selectedColumn = colmun;
			mediaList = getPieDataList(con);
			chartInfo = new PieStatisticData();
			chartInfo.setChartName("点击来源");
			chartResult = getChartResult(chartInfo, mediaList, "pie");
			// 媒体点击率数据表
			tableDataList.add(mediaList);
			// 媒体点击率chart图
			chartList.add(chartResult);
			
			// 各媒体广告位点击统计
			Iterator<StatisticData> lterator = mediaList.listIterator();
			PieStatisticData data = null;
			while(lterator.hasNext()) {
				data = (PieStatisticData)lterator.next();
				con = new SelectCondition();
				con.sql = "select ad_id,ad_name,ad_hitcount" +
						" from adposition" +
						" where projectcode='" + this.projectCode + "'" +
						" and m_id=" + data.getId();
				String[] colmun2 = {"ad_id", "ad_name", "ad_hitcount"};
				con.selectedColumn = colmun2;
				adList = getPieDataList(con);
				chartInfo = new PieStatisticData();
				chartInfo.setChartName(data.getName() + "广告位点击");
				chartResult = getChartResult(chartInfo, adList, "pie");
				tableDataList.add(adList);
				chartList.add(chartResult);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		detail.setReportName("点击来源报表");
		detail.setTableDataList(tableDataList);
		detail.setChartViewList(chartList);
		return detail;
	}

	@Override
	public ReportResult getRegisterSourceReport() {
		ReportResult detail = new ReportResult();
		List<ChartResult> chartList = new ArrayList<ChartResult>();
		SelectCondition con = null;
		PieStatisticData chartInfo = null;
		List<StatisticData> mediaList = null;
		List<StatisticData> adList = null;
		ChartResult chartResult = null;
		List<List<StatisticData>> tableDataList = new ArrayList<List<StatisticData>>();
		try {
			// 媒体注册统计
			con = new SelectCondition();
			con.sql = "select m.m_id,m.m_name,sum(a.ad_registercount) as registercount" +
					" from adposition a" +
					" left join media m" +
					" on a.m_id=m.m_id" +
					" where projectcode='" + this.projectCode + "'" +
					" group by m.m_id,m.m_name";
			String[] colmun = {"m_id","m_name","registercount"};
			con.selectedColumn = colmun;
			mediaList = getPieDataList(con);
			chartInfo = new PieStatisticData();
			chartInfo.setChartName("参与用户来源");
			chartResult = getChartResult(chartInfo, mediaList, "pie");
			tableDataList.add(mediaList);
			chartList.add(chartResult);
			// 各媒体广告位注册统计
			Iterator<StatisticData> lterator = mediaList.listIterator();
			PieStatisticData data = null;
			while(lterator.hasNext()) {
				data = (PieStatisticData)lterator.next();
				con = new SelectCondition();
				con.sql = "select ad_id,ad_name,ad_registercount" +
						" from adposition" +
						" where projectcode='" + this.projectCode + "'" +
						" and m_id=" + data.getId();
				String[] colmun2 = {"ad_id", "ad_name", "ad_registercount"};
				con.selectedColumn = colmun2;
				adList = getPieDataList(con);
				chartInfo = new PieStatisticData();
				chartInfo.setChartName(data.getName() + "广告位");
				chartResult = getChartResult(chartInfo, adList, "pie");
				tableDataList.add(adList);
				chartList.add(chartResult);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		detail.setReportName("参与用户来源报表");
		detail.setTableDataList(tableDataList);
		detail.setChartViewList(chartList);
		return detail;
	}

	@Override
	public ReportResult getClickRegisterCompareReport() {
		ReportResult detail = new ReportResult();
		List<ChartResult> chartList = new ArrayList<ChartResult>();
		SelectCondition con = null;
		PieStatisticData chartInfo = null;
		List<StatisticData> totalList = null;
		ChartResult chartResult = null;
		List<List<StatisticData>> tableDataList = new ArrayList<List<StatisticData>>();
		try {
			con = new SelectCondition();
			con.sql = "select 1 as id, '参与人数' as name, sum(ad_hitcount) as count from adposition" +
					" where projectcode='" + this.projectCode + "'" +
					" union" +
					" select 2 as id, '点击人数' as name, sum(ad_registercount) as count from adposition" +
					" where projectcode='" + this.projectCode + "'";
			String[] colmun = {"id","name","count"};
			con.selectedColumn = colmun;
			totalList = getPieDataList(con);
			chartInfo = new PieStatisticData();
			chartInfo.setChartName("参与 - 点击比较");
			chartResult = getChartResult(chartInfo, totalList, "pie");
			tableDataList.add(totalList);
			chartList.add(chartResult);
		} catch (IOException e) {
			e.printStackTrace();
		}
		detail.setReportName("点击与参与用户比较");
		detail.setTableDataList(tableDataList);
		detail.setChartViewList(chartList);
		return detail;
	}

	@Override
	public ReportResult getUserCityReport() {
		ReportResult detail = new ReportResult();
		List<ChartResult> chartList = new ArrayList<ChartResult>();
		SelectCondition con = null;
		BarStatisticData chartInfo = null;
		List<StatisticData> userCityList = null;
		ChartResult chartResult = null;
		List<List<StatisticData>> tableDataList = new ArrayList<List<StatisticData>>();
		try {
			con = new SelectCondition();
			con.sql = "select callerloc, count(callerloc) as count" +
					" from customer" +
					" where projectcode='" + this.projectCode + "'" +
					" and callerloc <> ''" +
					" group by callerloc";
			String[] colmun = {"callerloc","callerloc","count"};
			con.selectedColumn = colmun;
			userCityList = getBarDataList(con);
			chartInfo = new BarStatisticData();
			chartInfo.setChartName("用户城市分布");
			chartInfo.setCategoryAxisLabel("城市");
			chartInfo.setValueAxisLabel("人数");
			chartResult = getChartResult(chartInfo, userCityList, "bar");
			tableDataList.add(userCityList);
			chartList.add(chartResult);
		} catch (IOException e) {
			e.printStackTrace();
		}
		detail.setReportName("用户城市来源报表");
		detail.setTableDataList(tableDataList);
		detail.setChartViewList(chartList);
		return detail;
	}
	
	private ChartResult getChartResult(StatisticData chartInfo, List<StatisticData> list, String chartType) throws IOException {
		ChartHelper helper = new ChartHelper(chartInfo, list, request);
		String tempPath = "";
		if("pie" == chartType) {
			tempPath = helper.getPieChart();
		} else if("bar" == chartType) {
			tempPath = helper.getBarChart();
		}
		ChartResult chartResult = new ChartResult();
		chartResult.setChartName(chartInfo.getChartName());
		chartResult.setChartTempPath(tempPath);
		return chartResult;
	}
	
	@SuppressWarnings("unchecked")
	private List<StatisticData> getPieDataList(SelectCondition con) {
		String sql = con.sql;
		SQLProcessor sp = null;
		List<StatisticData> resultList = new ArrayList<StatisticData>();
		try{
			sp = new SQLProcessor();
			List<Map<String, Object>> list = sp.executeQuery(sql);
			for(int i=0; i<list.size(); i++){
				PieStatisticData data = new PieStatisticData();
				Map<String, Object> dataMap = list.get(i);
				data.setId(String.valueOf((Integer)dataMap.get(con.selectedColumn[0])));
				data.setName((String)dataMap.get(con.selectedColumn[1]));
				data.setCount((Integer)dataMap.get(con.selectedColumn[2]));
				resultList.add(data);
			}
			System.out.println("SQL: " + sql);
		}catch(SQLException se){
			System.out.println("Error SQL: " + sql);
			se.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			SQLProcessor.staticClose(sp);
		}
		return resultList;
	}
	
	@SuppressWarnings("unchecked")
	private List<StatisticData> getBarDataList(SelectCondition con) {
		String sql = con.sql;
		SQLProcessor sp = null;
		List<StatisticData> resultList = new ArrayList<StatisticData>();
		try{
			sp = new SQLProcessor();
			List<Map<String, Object>> list = sp.executeQuery(sql);
			for(int i=0; i<list.size(); i++){
				BarStatisticData data = new BarStatisticData();
				Map<String, Object> dataMap = list.get(i);
				data.setName((String)dataMap.get(con.selectedColumn[0]));
				data.setCategory((String)dataMap.get(con.selectedColumn[1]));
				data.setCount((Integer)dataMap.get(con.selectedColumn[2]));
				resultList.add(data);
			}
			System.out.println("SQL: " + sql);
		}catch(SQLException se){
			System.out.println("Error SQL: " + sql);
			se.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			SQLProcessor.staticClose(sp);
		}
		return resultList;
	}
	
	class SelectCondition {
		public String sql = "";
		public String[] selectedColumn = null;
	}

}
